package com.nzw.utils;

import ch.qos.logback.core.util.CloseUtil;

import java.io.*;

import static com.alibaba.fastjson.util.IOUtils.close;

/**
 * Created by Administrator on 2018/1/8/008.
 */



public class TranscoderUtils {



    public static byte[] serialize(Object value) {
        if (value == null) {

            System.out.println("Can't serialize null");
            throw new NullPointerException("Can't serialize null");

        }
        byte[] rv=null;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        try {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(value);
            os.close();
            bos.close();
            rv = bos.toByteArray();
        } catch (IOException e) {
            throw new IllegalArgumentException("Non-serializable object", e);
        } finally {
            close(os);
            close(bos);
        }
        return rv;
    }


    public static Object deserialize(byte[] in) {
        Object rv=null;
        ByteArrayInputStream bis = null;
        ObjectInputStream is = null;
        try {
            if(in != null) {
                bis=new ByteArrayInputStream(in);
                is=new ObjectInputStream(bis);
                rv=is.readObject();
                is.close();
                bis.close();

            }
        } catch (Exception e) {

            System.out.println(e);
        } finally {

            close(is);
            close(bis);

        }
        return rv;
    }
}




